package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ClassifyService;
import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.Classify;
import cn.bytecollege.common.entity.Merchant;
import cn.bytecollege.common.entity.Product;
import cn.bytecollege.common.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 商品添加控制器。
 */
@WebServlet(name = "ProductSaveController", value = "/admin/saveProduct")
public class ProductSaveController extends HttpServlet {
    private ProductService productService = null;
    private ClassifyService classifyService = null;
    public ProductSaveController(){
        productService = new ProductService();
        classifyService = new ClassifyService();
    }
    String msg = "";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("12121");
        //封装分类数据
        List<Classify> list = classifyService.findClassify();
        request.setAttribute("list",list);
//        response.sendRedirect(request.getContextPath()+"/manage/productSave.jsp");
        request.getRequestDispatcher(request.getContextPath()+"/admin/html/device/productSave.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收前台的参数
        String classifyId = request.getParameter("classify");
        String productName = request.getParameter("productName");
        //商品数量 int
        String productNumber = request.getParameter("productNumber");
        //商品价格 float
        String productPrice = request.getParameter("productPrice");
        //转型
        BigDecimal bigProductPrice = new BigDecimal(productPrice);
        //商品图片
        String productPicture = request.getParameter("poster");
        //商品详情
        String productParticulars = request.getParameter("productParticulars");

        //封装product
        Product product = new Product();
        product.setClassifyId(Integer.parseInt(classifyId));
        product.setProductName(productName);
        product.setProductAllnumber(Integer.parseInt(productNumber));
        product.setProductPrice(bigProductPrice);
        product.setProductPicture(productPicture);
        product.setProductParticulars(productParticulars);

        //先取出session中的值
        HttpSession session = request.getSession();
        Merchant merchant = (Merchant) session.getAttribute("merchant");
        //User user = (User) session.getAttribute("user");
        //再把merchantId存进去，就知道商家1的商品有哪些了。
        product.setMerchantId(merchant.getMerchantId());

        //添加商品之前先查看商品名称是否已经存在
        Product tempProduct = productService.findByProductName(productName);
        if (tempProduct != null){
            msg = "商品名称已经存在，请另改名称！";
            request.setAttribute("msg",msg);
            //跳回来
            request.getRequestDispatcher("/admin/saveProduct").forward(request,response);
            return;
        }
        productService.save(product);
        //跳转页面
        response.sendRedirect("/admin/productListMerchant");
    }
}
